package com.jash.encryption;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


/**
 * A simple {@link Fragment} subclass.
 */
public class DESFragment extends Fragment implements View.OnClickListener {


    private TextInputLayout key;
    private TextInputLayout src;
    private TextInputLayout rlt;
    private String algorithm = "AES";
    private int KEY_LENGTH = 32;

    public DESFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_de, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        key = ((TextInputLayout) view.findViewById(R.id.des_key));
        src = ((TextInputLayout) view.findViewById(R.id.des_src));
        rlt = ((TextInputLayout) view.findViewById(R.id.des_rlt));
        view.findViewById(R.id.des_encryption).setOnClickListener(this);
        view.findViewById(R.id.des_decryption).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String key_str = key.getEditText().getText().toString();
        if (TextUtils.isEmpty(key_str)) {
            key.setError("密钥不可为空");
            key.setErrorEnabled(true);
        } else {
            key.setErrorEnabled(false);
            byte[] bytes = Arrays.copyOf(key_str.getBytes(), KEY_LENGTH);
            SecretKeySpec des = new SecretKeySpec(bytes, algorithm);
            try {
                Cipher cipher = Cipher.getInstance(algorithm);
                switch (view.getId()) {
                    case R.id.des_encryption:
                        String src_str = src.getEditText().getText().toString();
                        if (TextUtils.isEmpty(src_str)) {
                            src.setError("加密时，原文不可为空");
                            src.setErrorEnabled(true);
                        } else {
                            src.setErrorEnabled(false);
                            cipher.init(Cipher.ENCRYPT_MODE, des);
                            byte[] encypt = cipher.doFinal(src_str.getBytes());
                            rlt.getEditText().setText(Base64.encodeToString(encypt, Base64.NO_WRAP));
                        }
                        break;
                    case R.id.des_decryption:
                        String rlt_str = rlt.getEditText().getText().toString();
                        if (TextUtils.isEmpty(rlt_str)) {
                            rlt.setError("解密时，密文不可为空");
                            rlt.setErrorEnabled(true);
                        } else {
                            rlt.setErrorEnabled(false);
                            cipher.init(Cipher.DECRYPT_MODE, des);
                            byte[] decrypt = cipher.doFinal(Base64.decode(rlt_str, Base64.NO_WRAP));
                            src.getEditText().setText(new String(decrypt));
                        }
                        break;
                }
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            }
        }
    }
}
